package com.hansen.library.ui.widget.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;

import com.hansen.library.R;
import com.hansen.library.utils.ColorUtils;

/**
 * 上边和下边有横线的TextView
 * Created by han on 2017/7/26 0026.
 */
public class BorderTextView extends android.support.v7.widget.AppCompatTextView {

    //需要左边界
    private boolean hasLeftBorder;
    //需要上边界
    private boolean hasTopBorder;
    //需要右边界
    private boolean hasRightBorder;
    //需要下边界
    private boolean hasBottomBorder;
    //中间
    private boolean hasCenterBorder;
    //中间横线只画textview padding的宽度
    private boolean mOnlyCenterPaddingBorder;
    //border 上下间距
    private int mBorderPadding;
    //border center 时文字和border间距
    private int mBorderCenterTextMargin;

    private Paint mPaint;
    private Path mPath;

    private int mBorderColor;
    private int mStartColor;
    private int mCenterColor;
    private int mEndColor;
    private int mBorderWidth;

    public BorderTextView(Context context) {
        this(context, null);
    }

    public BorderTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BorderTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mBorderWidth = 1;
        mBorderColor = ColorUtils.getColorById(context, R.color.color_d8d8d8);

        if(attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BorderLayoutStyle, defStyleAttr, 0);

            hasLeftBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_left, false);
            hasTopBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_top, false);
            hasRightBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_right, false);
            hasBottomBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_bottom, false);
            hasCenterBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_center, false);
            mOnlyCenterPaddingBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_center_only_padding, false);

            mBorderWidth = a.getDimensionPixelSize(R.styleable.BorderLayoutStyle_my_border_width, mBorderWidth);
            mBorderPadding = a.getDimensionPixelOffset(R.styleable.BorderLayoutStyle_my_border_padding, 0);
            mBorderCenterTextMargin = a.getDimensionPixelOffset(R.styleable.BorderLayoutStyle_my_border_center_text_margin, 0);

            mBorderColor = a.getColor(R.styleable.BorderLayoutStyle_my_border_color, mBorderColor);

            mStartColor = a.getColor(R.styleable.BorderLayoutStyle_my_border_start_color, 0);
            mCenterColor = a.getColor(R.styleable.BorderLayoutStyle_my_border_center_color, 0);
            mEndColor = a.getColor(R.styleable.BorderLayoutStyle_my_border_end_color, 0);

            a.recycle();
        }

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mBorderColor);
        mPaint.setStrokeWidth(mBorderWidth);
        mPaint.setAntiAlias(true);

        mPath = new Path();

        if(mStartColor != 0 && mCenterColor != 0 && mEndColor != 0) {
            int [] colors = {mStartColor, mCenterColor, mEndColor};
            float[] position = {0f, 0.5f, 1.0f};

            LinearGradient mLinearGradient = new LinearGradient(0, 0, 0, mBorderWidth, colors, position, Shader.TileMode.CLAMP);

            mPaint.setShader(mLinearGradient);
        } else if(mStartColor != 0 && mEndColor != 0) {
            LinearGradient mLinearGradient = new LinearGradient(0, 0, 0, mBorderWidth, mStartColor, mEndColor, Shader.TileMode.CLAMP);

            mPaint.setShader(mLinearGradient);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (hasLeftBorder) {
            mPath.reset();
            mPath.moveTo((float) (0 + mBorderWidth / 2.0), 0 + mBorderPadding);
            mPath.lineTo((float) (0 + mBorderWidth / 2.0), this.getHeight() - mBorderPadding);
            canvas.drawPath(mPath, mPaint);
        }

        if (hasTopBorder) {
            mPath.reset();
            mPath.moveTo(0 + mBorderPadding, (float) (0 + mBorderWidth / 2.0));
            mPath.lineTo(this.getWidth() - mBorderPadding, (float) (0 + mBorderWidth / 2.0));
            canvas.drawPath(mPath, mPaint);
        }

        if (hasRightBorder) {
            mPath.reset();
            mPath.moveTo((float) (this.getWidth() - mBorderWidth / 2.0), 0 + mBorderPadding);
            mPath.lineTo((float) (this.getWidth() - mBorderWidth / 2.0), this.getHeight() - mBorderPadding);
            canvas.drawPath(mPath, mPaint);
        }

        if (hasBottomBorder) {
            mPath.reset();
            mPath.moveTo(0 + mBorderPadding, (float) (this.getHeight() - mBorderWidth / 2.0));
            mPath.lineTo(this.getWidth() - mBorderPadding, (float) (this.getHeight() - mBorderWidth / 2.0));
            canvas.drawPath(mPath, mPaint);
        }
        if (hasCenterBorder) {
            if(mOnlyCenterPaddingBorder) {
                mPath.reset();
                mPath.moveTo(mBorderPadding, this.getHeight() / 2);
                mPath.lineTo(getPaddingLeft() - mBorderCenterTextMargin, this.getHeight() / 2);

                canvas.drawPath(mPath, mPaint);

                mPath.reset();
                mPath.moveTo(this.getWidth() - getPaddingRight() + mBorderCenterTextMargin, this.getHeight() / 2);
                mPath.lineTo(this.getWidth() - mBorderPadding, this.getHeight() / 2);

                canvas.drawPath(mPath, mPaint);
            } else {
                mPath.reset();
                mPath.moveTo(0, this.getHeight() / 2);
                mPath.lineTo(this.getWidth(), this.getHeight() / 2);

                canvas.drawPath(mPath, mPaint);
            }
        }
    }

    public void setBorderWidth(int mBorderWidth) {
        this.mBorderWidth = mBorderWidth;
        if(mPaint != null) {
            mPaint.setStrokeWidth(mBorderWidth);
        }

        postInvalidate();
    }

    public void setBorderColor(int mBorderColor) {
        this.mBorderColor = mBorderColor;
        if(mPaint != null) {
            mPaint.setColor(mBorderColor);
        }

        postInvalidate();
    }

    public void setBorderPadding(int mBorderPadding) {
        this.mBorderPadding = mBorderPadding;

        postInvalidate();
    }

    public void setHasBorder(boolean left, boolean top, boolean right, boolean bottom) {
        setHasBorder(left, top, right, bottom, false);
    }

    public void setHasBorder(boolean left, boolean top, boolean right, boolean bottom, boolean center) {
        this.hasLeftBorder = left;
        this.hasTopBorder = top;
        this.hasRightBorder = right;
        this.hasBottomBorder = bottom;
        this.hasCenterBorder = center;

        postInvalidate();
    }
}
